package cn.guozeyiblog.client;

import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WebViewActivity extends Activity implements OnClickListener {
	
	private ImageView backImage;
	private ImageView shareImage;
	private WebView web;
	private Intent intent;
	private String url;
	private ProgressBar bar;
	private TextView head_title;
	private RelativeLayout adlayout;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewactivity);
		initView();
		intent = getIntent();
		//head_title.setText(intent.getExtras().getString("title","网站Title"));
	    head_title.setText(intent.getStringExtra("title"));
	    //url = intent.getExtras().getString("url","http://www.guozeyiblog.cn");
	    url = intent.getStringExtra("url");
	    //------------------AD-----------------------------------------------
	    AdView adView = new AdView(getApplicationContext(), AdSize.FIT_SCREEN);
	    adlayout.addView(adView);
	    
	    web.loadUrl(url);
	    bar.setMax(100);
	    backImage.setOnClickListener(this);
	    shareImage.setOnClickListener(this);
	    web.getSettings().setJavaScriptEnabled(true);
	    web.setWebViewClient(new WebViewClient());
	    web.setWebChromeClient(new WebChromeClient(){
	    	@Override
	    	public void onProgressChanged(WebView view, int newProgress) {
	    		// TODO Auto-generated method stub
	    		bar.setVisibility(View.VISIBLE);
	    		bar.setProgress(newProgress);
	    		if (newProgress==100) {
					bar.setVisibility(View.GONE);
				}
	    		super.onProgressChanged(view, newProgress);
	    	}
	    });
		/**
		 * 加载数据到webview中
		 */
	}
	
	private void initView(){
		backImage = (ImageView) findViewById(R.id.head_back);
		shareImage = (ImageView) findViewById(R.id.head_share);
		web = (WebView) findViewById(R.id.web_view);
		bar = (ProgressBar) findViewById(R.id.web_progressBar1);
		head_title = (TextView) findViewById(R.id.head_title);
		adlayout = (RelativeLayout) findViewById(R.id.adLayout);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_back:
			 this.finish();
			break;
		case R.id.head_share:
             //Toast.makeText(getApplicationContext(), "分享功能", 0).show();
             Intent intent=new Intent(Intent.ACTION_SEND);
             intent.setType("text/plain");
             intent.putExtra(Intent.EXTRA_SUBJECT, "网页分享");
             intent.putExtra(Intent.EXTRA_TEXT, "这篇文章很不错,推荐给你看看>>"+web.getUrl());
             startActivity(Intent.createChooser(intent, getTitle()));
             
		default:
			break;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub 
		//Toast.makeText(getApplicationContext(), "Code"+keyCode, 0).show();
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(web.canGoBack()){
				web.goBack();
				return true;
			}
			return super.onKeyDown(keyCode, event);
		}else {
			return super.onKeyDown(keyCode, event);	
		}	
	}
}
